package ar.uba.fi.RSACryptoFiuba.model.decrypt;

import java.util.ArrayList;
import java.util.List;

import ar.uba.fi.RSACryptoFiuba.model.RSAAlgorithm;
import ar.uba.fi.RSACryptoFiuba.model.RSAAlgorithmStrategy;
import ar.uba.fi.RSACryptoFiuba.model.RSAMessage;
import ar.uba.fi.RSACryptoFiuba.model.RSAStep;
import ar.uba.fi.RSACryptoFiuba.model.keygeneration.VoidStep;

public class RSADecryptAlgorithm extends RSAAlgorithm{
	
	public RSADecryptAlgorithm(RSAAlgorithmStrategy strategy, RSAMessage message) {
		super(strategy, message);
	}

	private int decryptionSteps;
	
	@Override
	public void init() {
		
		RSADecryptMessage decryptMessage = ((RSADecryptMessage)this.getMessage());
		double l = decryptMessage.getNKey().length();
		double cLenght = decryptMessage.getEncryptedText().length();
		int chunks = (int) Math.ceil(cLenght / l);
		List<RSAStep> decryptSteps = new ArrayList<RSAStep>();
		for (int i = 0; i < chunks; i++) {
			decryptSteps.add(new DecryptStep());
		}
		
		for (int i = 0; i < chunks - 1; i++) {
			decryptSteps.get(i).setNext(decryptSteps.get(i+1));
		}
		
		for (int i = chunks-1; i > 0; i--) {
			decryptSteps.get(i).setPrevious(decryptSteps.get(i-1));
		}

		RSAStep convertNumericalRepresentationStep = new ConvertNumericalRepresentationStep();
		convertNumericalRepresentationStep.setPrevious(decryptSteps.get(chunks-1));
		decryptSteps.get(chunks-1).setNext(convertNumericalRepresentationStep);
		
		RSAStep resultsStep = new VoidStep();
		resultsStep.setPrevious(convertNumericalRepresentationStep);
		
		convertNumericalRepresentationStep.setNext(resultsStep);
		
		this.setCurrentStep(decryptSteps.get(0));
		this.decryptionSteps = chunks;
	}

	public int getDecryptionSteps() {
		return decryptionSteps;
	}

}
